package com.fengjue.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Configuration
public class GlobalFilter implements org.springframework.cloud.gateway.filter.GlobalFilter, Ordered {

    //这里可以用来鉴权，日志输出，限流
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("进入自定义的filter");
        if (exchange.getRequest().getQueryParams().get("username") != null) {
            // 获取请求方式
            HttpMethod method = exchange.getRequest().getMethod();
            System.out.println("method = " + method);
            System.out.println("用户名有效");
            return chain.filter(exchange); // 放行请求
        }
        System.out.println("非法用户，拒绝访问");





        return exchange.getResponse().setComplete();
    }



    @Override
    public int getOrder() {
        // filter执行顺序，值越小，优先级越高
        return -1;
    }
}
